﻿package cn.hadron.aa.util;
import java.util.List;
public class Pager<T> {
	private int pageNo = 1;       //当前页码
	private int pageSize = 10;    //一页显示的条数
	private int totalRows;        //总的记录数
	private int totalPages;       //总的页数
	private String url;	          //访问页URL
	private List<T> data;         //当前页的记录
	
	public Pager(int pageNo,int pageSize,int totalRows){//URL地址、当前页号、总记录数
		this.pageNo = pageNo<1?1:pageNo;
		this.pageSize=pageSize<1?1:pageSize;
		this.totalRows = totalRows<0?0:totalRows;
		// 计算总的页数
		if (this.totalRows % this.pageSize == 0) 
			this.totalPages = this.totalRows / pageSize;
		else
			this.totalPages = this.totalRows / pageSize+1;
    }

	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getTotalRows() {
		return totalRows;
	}
	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}
	public int getTotalPages() {
		return totalPages;
	}
	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
		String str="";
		//判定URL地址是否含"?"，也就是多个参数
		if(url.contains("?"))url+="&id="; 
		else url+="?id=";
		if(pageNo >=2) 
			str+=" <a href="+url+"1>首页</a>"; 
		else str += " 首页 ";
		if(pageNo >=3) 
			str+= " <a href="+url + (pageNo-1) + ">前页</a> "; 
		else str+= " 前页 ";
		if(pageNo < totalPages) 
			str+= " <a href="+url +(pageNo+1) + ">后页</a> "; 
		else str += " 后页 ";
		if(totalPages!=0 && pageNo < totalPages) 
			str+= " <a href=" +url + totalPages + ">尾页</a>"; 
		else str += " 尾页 ";
		this.url = str;
	}
	public List<T> getData() {
		return data;
	}
	public void setData(List<T> data) {
		this.data= data;
	}
}
